# import os
# import sys
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# sys.path.append(BASE_DIR)
# from message_forward.manger import user_manager
#接后续代码
import sched
import time
from datetime import datetime
from revChatGPT.V1 import Chatbot

# 初始化sched模块的scheduler类
# 第一个参数是一个可以返回时间戳的函数，第二个参数可以在定时未到达之前阻塞。
schedule = sched.scheduler(time.time, time.sleep)
chatbot = Chatbot(config={
            "email": "",
            "password": ""
        })

def task(inc):
    now = datetime.now()
    # print('系统当前时间戳：' + str(now.timestamp()))
    ts = now.strftime("%Y-%m-%d %H:%M:%S")
    clearTime = now.strftime("%H:%M")
    print(ts)
    print(clearTime)
    if clearTime == '00:00':
        conversations = chatbot.get_conversations(0, 1000, None)
        print('原会话数：' + str(len(conversations)))
        # num = 0
        if len(conversations) > 0:
            chatbot.clear_conversations()
        # for data in conversations:
        #     timeL = time.mktime(time.strptime(data['create_time'],'%Y-%m-%dT%H:%M:%S.%f'))
        #     print(data['id'] + '-会话创建当前时间戳：' + str(timeL))
        #     print(str((float(now.timestamp()) - float(timeL))))
        #     if (float(now.timestamp()) - float(timeL)) > float(10): 
                
        #         print('开始清理创建一小时的会话' + data['id'])
        #         chatbot.delete_conversation(data['id'])
        #         num = num + 1
        #     # his_conversation = chatbot.get_msg_history(data['id'], None)
        #     # print(his_conversation)
            print('剩余会话数：' + str(chatbot.get_conversations(0, 1000, None)))

    schedule.enter(inc, 0, task, (inc,))

def func(inc=10):
    # enter四个参数分别为：
    # 间隔事件、优先级（用于同时间到达的两个事件同时执行时定序）、被调用触发的函数、给该触发函数的参数（tuple形式）
    schedule.enter(0, 0, task, (inc,))
    schedule.run()

if __name__ == '__main__':
    import uvicorn
    # func()
    uvicorn.run('task:func', host='0.0.0.0', port=8021,
                reload=True,
                reload_delay=0.25,
                ws_ping_interval=99999,
                ws_ping_timeout=99999,
                timeout_keep_alive=99999
                )